package spe.ui.editor;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.ui.actions.ToggleGridAction;
import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.swt.widgets.Composite;


public abstract class MyScrollingGraphicalViewer extends ScrollingGraphicalViewer 
{
	private SPEEditor editor;
	
	public MyScrollingGraphicalViewer(SPEEditor edt)
	{
		editor = edt;
		editor.getSelectionSychronizer().addViewer(this);
		setKeyHandler(new GraphicalViewerKeyHandler(this));
		setRootEditPart(new ScalableFreeformRootEditPart());
		prepareGrid();
	}
	
	protected abstract void initEditPartFactory();
	
	private void prepareGrid() {
		getLayerManager().getLayer(LayerConstants.GRID_LAYER).setForegroundColor(ColorConstants.lightBlue);
		editor.getActionRegistry().registerAction(new ToggleGridAction(this));
	}
	
	public void initControl(Composite parent)
	{
		super.createControl(parent);
		getControl().setBackground(ColorConstants.white);
		GraphicalViewerContextMenuProvider provider = new GraphicalViewerContextMenuProvider(this, editor.getActionRegistry());
		setContextMenu(provider);
		editor.getSite().registerContextMenu("spe.editor.grahp.context", provider, this);
		editor.getEditDomain().addViewer(this);
		initEditPartFactory();
		//System.out.println(String.format("[MyScrollingGraphicalViewer] Diagram: %s", editor.getDiagram()));
		setContents(editor.getDiagram());
	}
	
	public SPEEditor getEditor()
	{
		return editor;
	}
}
